package com.leapp.sk.viewdelegate

import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
import android.widget.TabHost
import android.widget.TextView
import cc.vv.lkimcomponent.lkim.bean.LKIMConversation
import com.example.mvp.delegate.BaseAppDelegate
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.ui.activity.AppMainTableActivity
import com.example.im.server.im.IMChatService
import com.leapp.sk.view.FragementMyTabHost

/**
 * 主页视图托管层
 * @author  chaohaipeng  on 2018/7/2.
 */
class MainTableDelegate : BaseAppDelegate() {
    /**
     * 未读消息条数
     */
    var unReadMsgCount = 0
    var tv_main_unread: TextView? = null


    var mTabHost: FragementMyTabHost? = null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_main
    }

    /**
     * 关联tab和fragment
     */
    fun initTab(mIVSrcArray: IntArray, mFragmentArray: Array<Class<*>>, mTabNames: Array<String>) {
        if (mIVSrcArray.size < mFragmentArray.size || mTabNames.size < mFragmentArray.size) {
            return
        }
        mTabHost = getView<FragementMyTabHost>(R.id.ft_main_am)
        mTabHost?.setup(getActivity(), getActivity<AppCompatActivity>().supportFragmentManager, R.id.fl_am_relContent)
        // 得到fragment的个数
        val fragmentCount = mFragmentArray.size
        for (i in 0 until fragmentCount) {
            // 为每一个Tab按钮设置图标、文字和内容
            val tabSpec: TabHost.TabSpec? = mTabHost?.newTabSpec(mTabNames!![i])?.setIndicator(
                    getTabItemView(mIVSrcArray, mTabNames, i))
            // 去除分割线
            mTabHost?.tabWidget?.dividerDrawable = null
            // 将Tab按钮添加进Tab选项卡中
            if (tabSpec != null) {
                mTabHost?.addTab(tabSpec, mFragmentArray[i], null)
            }
        }
    }
    /**
     * 设置tabHost图标和文字
     */
    private fun getTabItemView(mIVSrcArray: IntArray, mTabNames: Array<String>, index: Int): View {
        val tabView = View.inflate(getActivity<AppMainTableActivity>(), R.layout.layout_tab_text, null)
        val imageView = tabView.findViewById<ImageView>(R.id.iv_main_tabHost)
        imageView.setImageResource(mIVSrcArray[index])
        val textView = tabView.findViewById<TextView>(R.id.tv_main_tabHost)
        if (index ==0) {
            tv_main_unread = tabView.findViewById<TextView>(R.id.tv_main_unread)
            tv_main_unread?.visibility=View.VISIBLE
        }else{
            tv_main_unread?.visibility=View.GONE
        }
        textView.text = mTabNames[index]
        return tabView
    }

    /**
     * 加载会话
     */
     fun loadConversations() {

        IMChatService.getInstance().initConversation(true, object : IMChatService.ConversationInter() {
            override fun success(imConversations: List<LKIMConversation>) {
                SKApplication.getInstance()?.mHandler?.post(object : Runnable {
                    override fun run() {
                        unReadMsgCount=0
                        for (i in imConversations.indices) {
                            unReadMsgCount += imConversations.get(i).unRreadMsgCount
                        }
                        if (unReadMsgCount > 0) {
                            tv_main_unread?.text = unReadMsgCount.toString()
                            tv_main_unread?.visibility = View.VISIBLE
                        } else {
                            tv_main_unread?.visibility = View.GONE
                        }
                    }
                })
            }

            override fun faile() {
            }
        })

    }

}